package com.wl.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wl.bbs.entity.User;
import com.wl.bbs.dao.UserMapper;
import com.wl.bbs.entity.UserEx;
import com.wl.bbs.service.BizException;
import com.wl.bbs.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User login(User user) throws BizException {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUname, user.getUname());
        lqw.eq(User::getUpass, user.getUpass());
        User dbUser = userMapper.selectOne(lqw);
        if(dbUser == null){
            throw new BizException("用户名或密码错误!", null);
        }
        return dbUser;
    }

    @Override
    public void reg(UserEx user, Errors errors) throws BizException {
        if(user.getUpass().equals(user.getUpass1()) == false){
            errors.rejectValue("upass1","0000",
                    "确认密码与密码不一致");
        }
        // 同名验证
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUname, user.getUname());
        if (count(lqw)>0) {
            errors.rejectValue("upass1","0001",
                    "该用户名已经注册!");
        }
        if (errors.hasErrors()) {
            throw new BizException("字段验证错误!", errors.getAllErrors());
        }
        user.setRegtime(new Timestamp(System.currentTimeMillis()));
        this.save(user);
    }

    @Override
    public int setp(User user) {
        return userMapper.updP(user);
    }

    @Override
    public List<User> selectUsers() {
        return userMapper.selectUsers();
    }

    @Override
    public List<User> findMemberByQuery(String uname, String email, Integer status) {
        return userMapper.findMemberByQuery(uname, email, status);
    }

    @Override
    public void uploadQuestion(User user, Errors errors) throws BizException {
        if (user.getQ1().isEmpty() || user.getQ1().isEmpty() ||
                user.getQ1().isEmpty() || user.getQ1().isEmpty()||
                user.getQ1().isEmpty()|| user.getQ1().isEmpty()) {
            throw new BizException("问题或答案不能为空", errors.getAllErrors());
        }
        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.eq(User::getUname, user.getUname());
        userMapper.update(user, luw);
    }
    public int updateHead(User myinfo,String head){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getId,myinfo.getId());
        myinfo.setHead(head);
        int update = userMapper.update(myinfo, lqw);
        return update;
    }

    @Override
    public User findByNa(String uname) {
        return userMapper.findByNa(uname);
    }

    public User getQuestion() {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getId, 2);
        return userMapper.selectOne(lqw);
    }
}